package com.buddy.sds.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.buddy.sds.auth.entity.SystemPermission;
import com.buddy.sds.common.mybatis.RedisCache;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ISystemPermissionMapper extends BaseMapper<SystemPermission> {

    @Select("select * from t_system_permission where permission_id in (select rp_permission from t_system_role_permission where rp_role=#{role_id})  order by permission_oid asc")
    List<SystemPermission> getPermissionListByRole(String role_id);

    @Select("select* from t_system_permission where permission_id in (select rp_permission from t_system_role_permission where rp_role=#{role_id}) and permission_pid=#{pid}  order by permission_oid asc")
    List<SystemPermission> getPermissionListByRoleAndPid(@Param("role_id") String role_id,@Param("pid") String pid);

    @Select("select * from t_system_permission where  permission_pid=#{pid} order by permission_oid asc")
    List<SystemPermission> getPermissionListBydPid(@Param("pid") String pid);


    @Select("select distinct * from t_system_permission where  permission_id in( SELECT rp_permission from t_system_role_permission where rp_role in( select ur_role from t_system_user_role where ur_user = #{user_id} ) ) and permission_pid = #{pid}   order by permission_oid asc")
    List<SystemPermission> getPermissionListByUserAndPid(@Param("user_id")String user_id,@Param("pid")String pid);

    @Select("select distinct * from t_system_permission where permission_id in( SELECT rp_permission from t_system_role_permission where rp_role in( select ur_role from t_system_user_role where ur_user = #{user_id} ) )  order by permission_oid asc")
    List<SystemPermission> getPermissionListByUser(@Param("user_id")String user_id);

    @Insert("insert into t_system_role_permission (rp_id,rp_role,rp_permission) values(#{uuid},#{role_id},#{p_id})")
    void addRolePermission(@Param("uuid")String uuid ,@Param("role_id") String role,@Param("p_id")String pid);

    @Delete("delete from t_system_role_permission where rp_role=#{role_id} and rp_permission=#{p_id}")
    void deleteRolePermissionByRoleAndPermission(@Param("role_id") String role, @Param("p_id")String pid);


    @Delete("delete from t_system_role_permission where rp_permission=#{p_id}")
    void deleteRolePermissionByPermission(@Param("p_id")String pid);

    @Update("update t_system_permission set permission_visible = #{visible} where permission_id=#{id}")
    boolean toggleVisible(@Param("id") String pid,@Param("visible") boolean visible);

    @Select("select permission_url from t_system_permission where permission_ignore = true")
    List<String> getIgnoreUrlList();

    @Update("update t_system_permission set permission_ignore = #{ignore} where permission_id=#{id}")
    boolean toggleIgnore(@Param("id") String pid,@Param("ignore") boolean ignore);

    @Update("update t_system_permission set permission_homepage = false where permission_homepage = true")
    void disableHomePage();

    @Select("select permission_url from t_system_permission where permission_homepage = true")
    String getHomePagePath();
}

